{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63bd29b7",
   "metadata": {},
   "source": [
    "# 课后：\n",
    "> 1. 什么是字符串？\n",
    "> 2. 字符串的一般表示形式？\n",
    "> 3. 字符串的运算有哪些？ 譬如：+ 是什么意思，取值，切片，索引的概念是什么？\n",
    "> 4. 字符串的一般方法？ 譬如：count(),in"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7533b095",
   "metadata": {},
   "source": [
    "## 1.字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c8c8b59",
   "metadata": {},
   "source": [
    "由数字、字母、下划线组成的一串字符。是编程语言中表示文本的数据类型 。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4843b56d",
   "metadata": {},
   "source": [
    "## 2.字符串的一般表示形式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c2cabd4",
   "metadata": {},
   "source": [
    "* 三种形式\n",
    "> 1.单引号 \"  \n",
    "> 2.双引号 \"\"  \n",
    "> 3.三引号 \"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f2e7231",
   "metadata": {},
   "source": [
    "## 3.字符号的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "217f1c6a",
   "metadata": {},
   "source": [
    "### 3.1   “+”的意思"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b447cad2",
   "metadata": {},
   "source": [
    "串联或组合两个字符串。例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04208a5b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloWorld\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello\"\n",
    "b = \"World\"\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cf474b0",
   "metadata": {},
   "source": [
    "### 3.2 取值的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e83d8328",
   "metadata": {},
   "source": [
    "字符串取单个值，可以规定取值的起点，终点，步长。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "46a7c04b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12345678\n"
     ]
    }
   ],
   "source": [
    "abc = '12345678'\n",
    "\n",
    "#语句  字符串[起始索引:结束索引:步长] \n",
    "print(abc[0:8:1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c5d5643",
   "metadata": {},
   "source": [
    "### 3.3 切片的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b8258fb",
   "metadata": {},
   "source": [
    "字符串取多个值，遵循**左闭右开**的策略，包含起始，不包含结束。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dd96fc2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc\n",
      "cdefgh\n"
     ]
    }
   ],
   "source": [
    "zfc = 'abcdefgh'  #对应的索引  a = 0 b =1 c = 2  d =3\n",
    "print(zfc[:3])   #起始下标为0 结束下标为3  步长为1\n",
    "print(zfc[2:])   #下标从2开始，一直取到最后，也就是从 c = 2开始,默认步长为1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71437c7a",
   "metadata": {},
   "source": [
    "### 3.4 索引的概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc88f2c6",
   "metadata": {},
   "source": [
    "有序列表中的位置,从左往右以0开始,从右往左以-1开始。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "264a295e",
   "metadata": {},
   "source": [
    "## 4. 字符串的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b747dc9c",
   "metadata": {},
   "source": [
    "* 1.字符串拼接：join\n",
    "\n",
    "6.格式化输出：format\n",
    "\n",
    "7.将字母大写：upper\n",
    "\n",
    "8.将字母转为小写：lower"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e1578a8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bill，Steve，Elon\n"
     ]
    }
   ],
   "source": [
    "A = (\"Bill\", \"Steve\", \"Elon\")\n",
    "x = \"，\".join(A)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f1218bf",
   "metadata": {},
   "source": [
    "* 2.检查字符串：in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cc1b98b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "txt = \"China is a great country\"\n",
    "x = \"is\" in txt\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64cdbc2d",
   "metadata": {},
   "source": [
    "* 3.查找元素的位置：find"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "40269084",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "txt = \"Hello,welcome to my world.\"\n",
    "x = txt.find(\"welcome\")\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f992c5c1",
   "metadata": {},
   "source": [
    "* 4.查找元素的个数：count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "195e3802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "txt = \"I love apples, apple are my favorite fruit\"\n",
    "x = txt.count(\"apple\")\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20542e0d",
   "metadata": {},
   "source": [
    "* 5.替换字符：replace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "49e7059c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Hi~ o(*￣▽￣*)ブ!\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(a.replace(\"World\", \"Hi~ o(*￣▽￣*)ブ\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3796aace",
   "metadata": {},
   "source": [
    "* 6.字符串分割：split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3e50d0ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', ' World!']\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(a.split(\",\")) # returns ['Hello', ' World!']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8451c002",
   "metadata": {},
   "source": [
    "* 7.格式化输出：format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a9d4881d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is Bill, and I am 19\n"
     ]
    }
   ],
   "source": [
    "age = 19 \n",
    "txt = \"My name is Bill, and I am {}\"\n",
    "print(txt.format(age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a570695c",
   "metadata": {},
   "source": [
    "* 8.将字母转为小写：lower"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2a459928",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(a.lower())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e00ec26",
   "metadata": {},
   "source": [
    "* 9.将字母大写：upper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5ee8642b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO, WORLD!\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(a.upper())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
